package com.umeox.watch.moto.contact.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.umeox.moto.common.clicklistener.OnNoDoubleClickListener;
import com.umeox.moto.watch.themes.widget.RoundedImageView;
import com.umeox.watch.moto.contact.R;
import com.umeox.watch.moto.contact.base.GlideApp;
import com.umeox.watch.moto.contact.model.ContactDetail;

import java.util.ArrayList;
import java.util.List;

public class ContactsAdapter extends RecyclerView.Adapter {

    public List<ContactDetail> getDataList() {
        return mDataList;
    }

    private List<ContactDetail> mDataList = new ArrayList<>();

    private Context mContext;
    private OnItemClickListener mOnItemClickListener;

    private boolean isPtt = false;

    public ContactsAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.item_contact_record, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Holder viewHolder = (Holder) holder;
        viewHolder.bind(mDataList.get(position));
    }

    @Override
    public int getItemCount() {
        if (mDataList == null || mDataList.size() == 0) return 0;
        return mDataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public void setDataList(List<ContactDetail> dataList, boolean isPTT) {
        this.isPtt = isPTT;
        mDataList.clear();
        mDataList.addAll(dataList);
        notifyDataSetChanged();
    }


    private class Holder extends RecyclerView.ViewHolder {

        private TextView mNameTv;
        private TextView mLastTimeTv;
        private RoundedImageView mIconIv;
        private View mItemView;


        public Holder(@NonNull View itemView) {
            super(itemView);
            mNameTv = itemView.findViewById(R.id.tvName);
            mLastTimeTv = itemView.findViewById(R.id.tvLastContactTime);
            mIconIv = itemView.findViewById(R.id.ivAvatar);
            mItemView = itemView;
        }


        public void bind(ContactDetail contactDetail) {
            mNameTv.setText(contactDetail.getContactName());

//            if (!StringUtils.isEmpty(contactDetail.getLastTimeContacted()) && !isPtt) {
//                mLastTimeTv.setVisibility(View.VISIBLE);
//                mLastTimeTv.setText(contactDetail.getLastTimeContacted());
//                Drawable right = null;
//                if (contactDetail.isMissed()) {
//                    right = ResourceManager.getInstance().getDrawable(R.drawable.contact_call_missed);
//                }
//                mLastTimeTv.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, right, null);
//            } else {
//                mLastTimeTv.setVisibility(View.GONE);
//            }
            if (contactDetail.isAdmin()) {
                mIconIv.setBorderWidth(2.0f);
                mIconIv.setBorderColor(mContext.getResources().getColor(R.color.light_navigationColor));
            } else {
                mIconIv.setBorderWidth(0.0f);
            }

            if (TextUtils.isEmpty(contactDetail.getContactPhotoUrl())) {
                GlideApp.with(mContext)
                        .load(R.drawable.default_avatar)
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                        .into(mIconIv);
            } else {
                GlideApp.with(mContext)
                        .load(contactDetail.getContactPhotoUrl())
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                        .into(mIconIv);
            }
            mItemView.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(contactDetail);
                    }
                }
            });
        }
    }


    public interface OnItemClickListener {
        void onItemClick(ContactDetail contactDetail);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
